package com.tools.developer.fragments;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.tools.developer.R;
import com.tools.developer.app.AppCatalogAdapter;
import com.tools.developer.app.AppModel;

public class AppCategoryFragment extends ListFragment implements
		OnItemClickListener, OnClickListener {

	private ListView catalogList;
	private int mCatalogType = AppModel.APP_TYPE_ALL;
	private AppCatalogAdapter mCatalogAdapter;
	private Context mCtx;

	public AppCategoryFragment(Context ctx) {
		super();
		this.mCtx = ctx;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View contentView = inflater.inflate(
				R.layout.fragment_app_category_list, null);
		catalogList = (ListView) contentView.findViewById(android.R.id.list);
		mCatalogAdapter = new AppCatalogAdapter(mCtx);
		catalogList.setAdapter(mCatalogAdapter);
		catalogList.setOnItemClickListener(this);

		return contentView;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		if (parent == catalogList) {
			Toast.makeText(mCtx, mCatalogAdapter.mTitles.get(position),
					Toast.LENGTH_LONG).show();

			if (this.getResources().getString(R.string.app_catalog_all)
					.equalsIgnoreCase(mCatalogAdapter.mTitles.get(position))) {
				mCatalogType = AppModel.APP_TYPE_ALL;
			} else if (this.getResources()
					.getString(R.string.app_catalog_running)
					.equalsIgnoreCase(mCatalogAdapter.mTitles.get(position))) {
				mCatalogType = AppModel.APP_TYPE_RUNNING;
			} else if (this.getResources()
					.getString(R.string.app_catalog_sys_app)
					.equalsIgnoreCase(mCatalogAdapter.mTitles.get(position))) {
				mCatalogType = AppModel.APP_TYPE_SYS;
			} else {
				mCatalogType = AppModel.APP_TYPE_ALL;
			}
			if (mCategoryListener != null) {
				mCategoryListener.onCategoryClick(mCatalogType, null);
			}
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		}

	}

	private CategoryListener mCategoryListener;

	public void setCategoryListener(CategoryListener categoryListener) {
		mCategoryListener = categoryListener;
	}

	public interface CategoryListener {
		void onCategoryClick(int categoryType, Object extraData);
	}
}